package com.han.jdbc.mybatis._05;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import com.han.jdbc.mybatis._05.mappers.AssetMaterialMapper;
import com.han.jdbc.mybatis._05.mappers.SysSimcardMapper;
import com.han.jdbc.mybatis.generate.pojo.AssetMaterial;
import com.han.jdbc.mybatis.generate.pojo.SysSimcard;



/**
* @author hanpeng
* @version 创建时间：2020年9月4日 下午9:15:42
* @ClassName 类名称
* @Description 类描述
*/
public class TestM {
	public static void main(String[] args) {
		String resource="06mybatis-config.xml";
		SqlSession sqlSession=null;
		try {
			InputStream is = Resources.getResourceAsStream(resource);
			SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(is);
			sqlSession = sqlSessionFactory.openSession(false);
			SysSimcardMapper mapper = sqlSession.getMapper(SysSimcardMapper.class);
			AssetMaterialMapper assetMaterialMapper = sqlSession.getMapper(AssetMaterialMapper.class);
			List<SysSimcard> selectByLimit = mapper.selectByLimit(0, 100);
			System.out.println(selectByLimit.size());
			int  total=0;
			int  totalType=0;
			for(SysSimcard temp:selectByLimit) {
				Integer materialId = temp.getMaterialId();
				System.out.println(materialId);
				AssetMaterial assetMaterial = assetMaterialMapper.selectByPrimaryKey(materialId);
				if(assetMaterial.getId()!=null) {
					total++;
				}
				if(assetMaterial.getTypeId()!=null) {
					totalType++;
				}
				temp.setSimcardType(assetMaterial.getTypeId());
				int result = mapper.update(temp);
				System.out.println(result);
				
			}
			//System.out.println(total);
			//System.out.println("typeId总数为===> "+totalType);
			//查询所有
			//查询单条对应的typeid
			//然后单条的更新simcard_type
			
			//SysSimcard sysSimcard = mapper.selectByPrimaryKey(1);
			//System.out.println(sysSimcard.getCardNo());
			sqlSession.commit();
		} catch (IOException e) {
			e.printStackTrace();
			sqlSession.rollback();
		} finally {
			sqlSession.close();
		}
	}
	
}
